import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout, QLineEdit, 
                            QPushButton, QLabel, QFormLayout, QSpacerItem,
                            QSizePolicy)
from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtGui import QColor, QLinearGradient, QPainter, QFont, QPen
import datetime
from dao.classRecord import classRecord
from components.messageBox import CustomMessageBox

class ModernStudentForm(QWidget):
    def __init__(self,exam_info):
        super().__init__()
        self.exam = exam_info
        self.initUI()
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.oldPos = self.pos()
        self.setAttribute(Qt.WA_TranslucentBackground)  # 启用透明背景

    def initUI(self):
        self.setFixedSize(400, 600)
        
        # 主布局
        main_layout = QVBoxLayout()
        main_layout.setContentsMargins(40, 40, 40, 40)
        main_layout.setSpacing(25)
        
        # 顶部占位
        main_layout.addSpacerItem(QSpacerItem(20, 60, QSizePolicy.Minimum, QSizePolicy.Expanding))

        # 表单布局（去背景）
        form_layout = QFormLayout()
        form_layout.setVerticalSpacing(25)
        form_layout.setLabelAlignment(Qt.AlignLeft)
        form_layout.setContentsMargins(0, 0, 0, 0)

        # 输入框样式
        input_style = """
            QLineEdit {
                border: 2px solid #e0e0e0;
                border-radius: 8px;
                padding: 12px;
                font-size: 14px;
                background-color: rgba(255, 255, 255, 0.9);
            }
            QLineEdit:focus {
                border-color: #87CEEB;
                background-color: white;
            }
        """

        # 学生学号
        self.student_id = QLineEdit()
        self.student_id.setPlaceholderText("输入学号")
        self.student_id.setStyleSheet(input_style)
        form_layout.addRow(QLabel("学号"), self.student_id)

        # 学生姓名
        self.student_name = QLineEdit()
        self.student_name.setPlaceholderText("输入姓名")
        self.student_name.setStyleSheet(input_style)
        form_layout.addRow(QLabel("姓名"), self.student_name)

        # 教师姓名
        self.teacher_name = QLineEdit()
        self.teacher_name.setPlaceholderText("输入教师姓名")
        self.teacher_name.setStyleSheet(input_style)
        form_layout.addRow(QLabel("教师"), self.teacher_name)

        main_layout.addLayout(form_layout)

        # 底部占位
        main_layout.addSpacerItem(QSpacerItem(20, 60, QSizePolicy.Minimum, QSizePolicy.Expanding))

        # 按钮样式
        self.submit_btn = QPushButton("提交信息")
        self.submit_btn.setObjectName("submitBtn")
        self.submit_btn.setFixedHeight(45)
        self.submit_btn.clicked.connect(self.handle_submit)
        main_layout.addWidget(self.submit_btn)

        self.setLayout(main_layout)

        # 全局样式
        self.setStyleSheet("""
            QLabel {
                color: #444444;
                font-size: 14px;
                font-weight: 500;
                margin-bottom: 5px;
            }
            
            #submitBtn {
                background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                    stop:0 #6A5ACD, stop:1 #87CEEB);
                color: white;
                border: none;
                border-radius: 22px;
                font-size: 16px;
                font-weight: 500;
            }
            
            #submitBtn:hover {
                background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                    stop:0 #7B68EE, stop:1 #98FB98);
                box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
            }
        """)

    def handle_submit(self):
        formData = {
            'student_number':  self.student_id.text(),
            'student_name' : self.student_name.text(),
            'teacher_name' : self.teacher_name.text(),
            'exam_name' : self.exam['name'],
            'exam_class' : self.exam['class'],
            'exam_start_time' : self.exam['start'],
            'sign_in_time': datetime.datetime.now()
        }
        classRecord().insert_cord(formData)
        CustomMessageBox().show_success("补签成功")
        self.close()

    def mousePressEvent(self, event):
        self.oldPos = event.globalPos()

    def mouseMoveEvent(self, event):
        delta = QPoint(event.globalPos() - self.oldPos)
        self.move(self.x() + delta.x(), self.y() + delta.y())
        self.oldPos = event.globalPos()

    def paintEvent(self, event):
        # 绘制圆角窗口
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setPen(Qt.NoPen)
        
        # 主背景
        gradient = QLinearGradient(0, 0, self.width(), self.height())
        gradient.setColorAt(0, QColor(135, 206, 250))
        gradient.setColorAt(1, QColor(221, 160, 221))
        painter.setBrush(gradient)
        
        # 绘制带圆角的矩形
        rect = self.rect().adjusted(1, 1, -1, -1)  # 留出1像素边框
        painter.drawRoundedRect(rect, 15, 15)  # 15px圆角
